package com.st.lotte.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.st.lotte.dao.TLO_DELY_TRCE_RECV_IF_DAO;
import com.st.lotte.model.TLO_DELY_TRCE_RECV_IF;

public class Controller extends HttpServlet{
	Logger log = LoggerFactory.getLogger(Controller.class);
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		process(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		process(req,resp);
	}
	
	public String alert(String msg){
		String str ="<script>";
		str+="alert('"+msg+"'); ";
		str+="</script>";
		return str;
	}
	
	public void forward(HttpServletRequest req, HttpServletResponse resp, String url){
		try {
			RequestDispatcher dispatcher = req.getRequestDispatcher(url);
			dispatcher.forward(req,resp);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void process(HttpServletRequest req, HttpServletResponse resp) {
		log.debug("[controller]- start!!");
		TLO_DELY_TRCE_RECV_IF_DAO dao = new TLO_DELY_TRCE_RECV_IF_DAO();
		HttpSession session = req.getSession();
		
		try {
			req.setCharacterEncoding("euc-kr");
			resp.setContentType("text/html; charset=euc-kr");
			PrintWriter out = resp.getWriter();
			
			String command = req.getParameter("command");
			
			// login 처리
			System.out.println("command---"+command);
			if (command.equalsIgnoreCase("login")) {
				log.debug("[controller - login ]- start!!");
				String username = req.getParameter("username");
				String password = req.getParameter("password");
				if (username.equals("admin") && password.equals("lotte!23")) {
					session.setAttribute("loginID", username);
					forward(req, resp, "/controller?command=late");
				}else{
					out.print(alert("아이디와 패스워드가 정확하지 않습니다."));
				}
				log.debug("[controller - login ]- end!!");
			// log out 처리
			}else if(command.equalsIgnoreCase("logout")){
				log.debug("[controller - logout ]- start!!");
				out.print(alert("로그 아웃되었습니다."));
				session.invalidate();
				forward(req, resp, "login.jsp");
				log.debug("[controller - logout ]- end!!");
				
			// 발송지연처리 - login 처리하고 나서 첫화면
			}else if(command.equalsIgnoreCase("late")){
				log.debug("[controller - late.jsp ]- start!!");
//				String sDate = req.getParameter("sDate");
//				log.debug("[controller - late.jsp ]- 1!");
//				String eDate = req.getParameter("eDate");
//				log.debug("[controller - late.jsp ]- 2!");
//				
//				if (sDate==null || sDate.endsWith("")) {
//					sDate = "2014-11-10";
//				}
//				if (eDate==null || eDate.endsWith("")) {
//					eDate = "2014-11-11";
//				}
				String str ="and RECV_DATE>='2014-11-10' and RECV_DATE<='2014-11-11' limit 100";
				log.debug("[controller - late.jsp ]- 3! ");
				
				Vector<TLO_DELY_TRCE_RECV_IF> list = dao.selectNotDeliveryData(str, "gubun");
				log.debug("[controller - late.jsp ]- 5!");
				req.setAttribute("recvDTOList", list);
				forward(req, resp, "late.jsp");
				log.debug("[controller - late.jsp ]- end!!");
			// 미발송
			}else if (command.equalsIgnoreCase("notcomplete")){
				forward(req, resp, "notcomplete.jsp");
			}else if(command.equalsIgnoreCase("ratebyorder")){
				forward(req, resp, "ratebyorder.jsp");
			}else if(command.equalsIgnoreCase("ratebystart")){
				forward(req, resp, "ratebystart");
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
